package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.BeeFramework.Utils.Utils;
import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.ToastView;
import com.sgr.dykdriver.R;
import com.example.moshudriver.model.UserModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.userverifycodeResponse;
import com.example.moshudriver.utils.MyApplication;
import com.external.androidquery.callback.AjaxStatus;

//找回密码短信验证
public class B0_FindPassWorld extends Activity implements BusinessResponse,
		OnClickListener {

	private EditText b0_et_mobile;
	private Button mGetVerifyCodeAgain;
	private UserModel mUserModel;
	private ImageView top_view_back;
	private TextView top_view_title;
	private Button mNext;
	private EditText mVerifyCode;
	private TimeCount mTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.b0_find_pass_world);
		mUserModel = new UserModel(this);
		mUserModel.addResponseListener(this);
		top_view_title = (TextView) findViewById(R.id.top_view_title);
		top_view_title.setText("找回密码");
		top_view_back = (ImageView) findViewById(R.id.top_view_back);
		mVerifyCode = (EditText) findViewById(R.id.b0_et_verify_code);
		b0_et_mobile = (EditText) findViewById(R.id.b0_et_mobile);
		mNext = (Button) findViewById(R.id.b0_btn_next);
		mGetVerifyCodeAgain = (Button) findViewById(R.id.b0_btn_get_verify_code_again);

		mGetVerifyCodeAgain.setOnClickListener(this);
		top_view_back.setOnClickListener(this);
		mNext.setOnClickListener(this);
		MyApplication.getInstance().addActivity(this);
	}

	// 关闭键盘
	private void CloseKeyBoard() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(b0_et_mobile.getWindowToken(), 0);
	}

	/* 定义一个倒计时的内部类 */
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			mGetVerifyCodeAgain
					.setText(getString(R.string.get_verify_code_again));
			mGetVerifyCodeAgain.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			mGetVerifyCodeAgain.setClickable(false);
			mGetVerifyCodeAgain.setText(millisUntilFinished / 1000
					+ getString(R.string.resend_after));
		}
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ApiInterface.USER_VERIFYCODE)) {
			userverifycodeResponse response = new userverifycodeResponse();
			response.fromJson(jo);
			if (response.succeed == 1) {
				mTime = new TimeCount(60000, 1000);
				mTime.start();
			} else {
				b0_et_mobile.requestFocus();
			}
		} else if (url.endsWith(ApiInterface.USER_VALIDCODE)) {

			Intent intent = new Intent(this, B0_reset_password.class);// 注册界面
			intent.putExtra(B0_reset_password.MOBILE, b0_et_mobile.getText()
					.toString());
			startActivity(intent);
		}

	}

	@Override
	public void onClick(View v) {
		String mobile = b0_et_mobile.getText().toString();
		String verify_code = mVerifyCode.getText().toString().trim();
		switch (v.getId()) {
		case R.id.top_view_back:
			finish();
			break;
		case R.id.b0_btn_get_verify_code_again:
			if ("".equals(mobile)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				b0_et_mobile.requestFocus();
			} else if (mobile.length() < 11 || !Utils.isMobile(mobile)) {
				ToastView toast = new ToastView(this,
						getString(R.string.wrong_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				b0_et_mobile.requestFocus();
			} else {// 这个地方获取短信验证码并且发送

				mUserModel.getVerifyCode(mobile);// 获取短信验证码

				ToastView toast = new ToastView(this, "短信验证码已经发送");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mVerifyCode.requestFocus();
			}
			break;
		case R.id.b0_btn_next:
			if ("".equals(mobile)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				b0_et_mobile.requestFocus();
			} else if (mobile.length() < 11 || !Utils.isMobile(mobile)) {
				ToastView toast = new ToastView(this,
						getString(R.string.wrong_mobile_phone));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				b0_et_mobile.requestFocus();
			} else if ("".equals(verify_code)) {
				ToastView toast = new ToastView(this,
						getString(R.string.please_input_verify_code));
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mVerifyCode.setText("");
				mVerifyCode.requestFocus();
			} else if (verify_code.length() < 6) {
				ToastView toast = new ToastView(this, "输入6位的短信验证码");
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				mVerifyCode.requestFocus();
			}
			// 短信验证
			else {
				CloseKeyBoard();
				mUserModel.valid_verifycode(b0_et_mobile.getText().toString(),
						verify_code);
			}
			// 测试用
			// else if ("123456".equals(verify_code)) {
			// Intent intent=new Intent(this,B0_reset_password.class);//注册界面
			// intent.putExtra(B0_reset_password.MOBILE,
			// b0_et_mobile.getText().toString());
			// startActivity(intent);
			// }
			break;
		}
	}
}
